var pwait = "<div class='pwait'>正在检测请稍候。。。</div>";
var conflictArr = [];
var isConflictLoaded = false;
var isDeleted = false;//是否在规则列表中删除单个规则
//从排课页面第一次打开的时候，检测一下冲突
function checkAndShow(taskId) {
    showModal();
    if (isConflictLoaded == false) {
        showInfo(pwait);
        jQuery.ajax({
            url: "/m/conflict/check?taskId=" + taskId.toString(),
            success: function (ret) {
                var result = eval("(" + ret + ")");
                conflictArr = result;
                showConflict(result);
                isConflictLoaded = true;
            }
        });
    }
}
function autoCheck(taskId) {
    showInfo(pwait);
    jQuery.ajax({
        url: "/m/conflict/check?taskId=" + taskId.toString(),
        success: function (ret) {
            var result = eval("(" + ret + ")");
            conflictArr = result;
            showConflict(result);
            isConflictLoaded = true;
        }
    });
}

function checkConflict(taskId, afterCheckRun) {
    showInfo(pwait);
    jQuery.ajax({
        url: "/m/conflict/check?taskId=" + taskId.toString(),
        success: function (ret) {
            var result = eval("(" + ret + ")");
            conflictArr = result;
            
            // 检查冲突，根据冲突情况，更新任务状态
            checkStatus(result);

            // 显示冲突结果
            showConflict(result);

            if(afterCheckRun){
                try{
                    afterCheckRun();
                }catch(e){

                }
            }
        }
    });
}

/**
 * 检查冲突，根据冲突情况，更新任务状态
 * @param result
 */
function checkStatus(result) {
    // 有冲突，更新任务状态
    if (result.length > 0) {
        updateTaskStatus(3);
    }
    else {
        // 没冲突，成功
        updateTaskStatus(2);
    }
}

var b = "\r\n";
function showConflict(conflictList) {
    if (conflictList.length > 0) {
        var table = "<table class='conflictInfo'>" + b;
        table += "<tr><th>序号</th><th>冲突描述</th></tr>" + b;
        for (var i = 0; i < conflictList.length; i++) {
            var txt = conflictList[i].details
            table += "<tr><th>" + (i + 1).toString() + "</th>";
            table += "<td>" + txt + "</td></tr>" + b;
        }
        table += "</table>" + b;
        showResult(table);
    }
    else {
        var info = "<div class='pwait'>没有检测到冲突！</div>";
        showInfo(info);
    }
    $("#spcheck").html("(" + conflictList.length.toString() + ")");//冲突数量
}

function showModal() {
    $('#conflictModal').modal('show');
}

$('#conflictModal').on('show.bs.modal', function (e) {
    $(this).find('.modal-dialog').css({
        'margin-top': function () {
            var modalHeight = 450;
            return ($(window).height() - modalHeight ) / 2;
        }
    });
});

/**
 * 更新任务状态
 * @param status
 */
function updateTaskStatus(status) {
    jQuery.ajax({
        url: "/m/conflict/updateStatus?taskId=" + taskId.toString() + "&status=" + status,
        success: function (ret) {

        }
    });
}

function showInfo(html) {
    $("#divConflict").css("display", "table");
    $("#divConflict").html(html);
}
function showResult(html) {
    $("#divConflict").css("display", "block");
    $("#divConflict").html(html);
}

//function getRuleList() {
//    //如果有规则没保存先保存
//    // generateResult();
//    //展示
//    var unScheduleRuleType = $("#unScheduleRuleListTypeSlt").val();
//    jQuery.ajax({
//        type: "GET",
//        url: "/m/rule/getRuleList?taskId=" + taskId + "&unScheduleRuleType=" + unScheduleRuleType,
//        async: false,
//        success: function (msg) {
//            var message = JSON.parse(msg);
//            var retData = message.data;
//            if (retData != null) {
//                var length = retData.length;
//                var tableBodyHtml = new Array();
//                for (var i = 0; i < length; i++) {
//                    tableBodyHtml.push("<tr id='td" + retData[i].ruleItemId + "'>");
//                    tableBodyHtml.push("<td>" + retData[i].ruleTypeName + "</td>");
//                    tableBodyHtml.push("<td>" + retData[i].ruleContent + "</td>");
//                    tableBodyHtml.push("<td><a href='javascript:deleteRule(\"" + retData[i].ruleType + "\",\"" + retData[i].ruleItemId + "\")'>删除</a></td>");
//                    tableBodyHtml.push("</tr>");
//                }
//                $("#ruleListTable_body").html(tableBodyHtml.join(""));
//                $("#ruleListTable_body tr").css({
//                    "height" : "60px"
//                });
//                $("#ruleListTable_body td").css({
//                    "font-size" : "15px"
//                });
//            }
//        }
//    });
//    $("#ruleListModal").modal('show');
//}
//
///*点击规则列表删除规则*/
//function deleteRule(ruleType, ruleItemId) {
//    var postData = {"taskId": taskId, "unScheduleType": ruleType, "ruleItemId": ruleItemId};
//    jQuery.ajax({
//        type: "POST",
//        url: "/m/rule/deleteRule",
//        async: false,
//        data: postData,
//        success: function (msg) {
//            var retData = JSON.parse(msg);
//            if (retData == true) {
//                //控制样式上不显示
//                $("#td" + ruleItemId).hide();
//                isDeleted = true;
//            }
//        }
//    });
//}
//
///*规则列表框关闭事件*/
//$('#ruleListModal').on('hidden.bs.modal', function () {
//    //如果删除过规则，则重新加载页面，刷新数据
//    if (isDeleted == true) {
//        reloadPage();
//    }
//})
//
//// 添加、复用弹出窗口居中
//$('#ruleListModal').on('show.bs.modal', function (e) {
//    $(this).find('.modal-dialog').css({
//        'margin-top': function () {
//            var modalHeight = 540; // $('#myModal').find('.modal-dialog').height();
//            return ($(window).height() / 2 - (modalHeight / 2));
//        }
//    });
//});

function reloadPage() {
    location.reload();
    $("#unScheduleRuleTypeSlt").val('class');
    isDeleted = false;
}